package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharTeleportList;
import org.gdstash.character.GDCharUID;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.GDAbstractRiftPane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

public class RoTRiftAct6Pane extends GDAbstractRiftPane {
    private JCheckBox cbShatteredConclave;
    private JCheckBox cbTristram;
    private JCheckBox cbLevel02;
    private JCheckBox cbLevel04;
    private JCheckBox cbLevel06;
    private JCheckBox cbLevel08;
    private JCheckBox cbLevel10;
    private JCheckBox cbLevel12;
    private JCheckBox cbLevel14;
    private JCheckBox cbLevel16;
    private JCheckBox cbHoradrimTomb;
    private int difficulty;
    private boolean changed;

    public RoTRiftAct6Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbShatteredConclave).addComponent(this.cbTristram).addComponent(this.cbLevel02).addComponent(this.cbLevel04).addComponent(this.cbLevel06).addComponent(this.cbLevel08).addComponent(this.cbLevel10).addComponent(this.cbLevel12).addComponent(this.cbLevel14).addComponent(this.cbLevel16).addComponent(this.cbHoradrimTomb));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbShatteredConclave)).addGroup(layout.createParallelGroup().addComponent(this.cbTristram)).addGroup(layout.createParallelGroup().addComponent(this.cbLevel02)).addGroup(layout.createParallelGroup().addComponent(this.cbLevel04)).addGroup(layout.createParallelGroup().addComponent(this.cbLevel06)).addGroup(layout.createParallelGroup().addComponent(this.cbLevel08)).addGroup(layout.createParallelGroup().addComponent(this.cbLevel10)).addGroup(layout.createParallelGroup().addComponent(this.cbLevel12)).addGroup(layout.createParallelGroup().addComponent(this.cbLevel14)).addGroup(layout.createParallelGroup().addComponent(this.cbLevel16)).addGroup(layout.createParallelGroup().addComponent(this.cbHoradrimTomb));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbTristram, this.cbLevel02);
        layout.linkSize(0, this.cbTristram, this.cbLevel04);
        layout.linkSize(0, this.cbTristram, this.cbLevel06);
        layout.linkSize(0, this.cbTristram, this.cbLevel08);
        layout.linkSize(0, this.cbTristram, this.cbLevel10);
        layout.linkSize(0, this.cbTristram, this.cbLevel12);
        layout.linkSize(0, this.cbTristram, this.cbLevel14);
        layout.linkSize(0, this.cbTristram, this.cbLevel16);
        layout.linkSize(0, this.cbTristram, this.cbHoradrimTomb);
        layout.linkSize(1, this.cbTristram, this.cbLevel02);
        layout.linkSize(1, this.cbTristram, this.cbLevel04);
        layout.linkSize(1, this.cbTristram, this.cbLevel06);
        layout.linkSize(1, this.cbTristram, this.cbLevel08);
        layout.linkSize(1, this.cbTristram, this.cbLevel10);
        layout.linkSize(1, this.cbTristram, this.cbLevel12);
        layout.linkSize(1, this.cbTristram, this.cbLevel14);
        layout.linkSize(1, this.cbTristram, this.cbLevel16);
        layout.linkSize(1, this.cbTristram, this.cbHoradrimTomb);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ACT6"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbShatteredConclave == null) {
            (this.cbShatteredConclave = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbShatteredConclave.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_06_SHATTERED_CONCLAVE"));
        this.cbShatteredConclave.setFont(fntCheck);
        if (this.cbTristram == null) {
            (this.cbTristram = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbTristram.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_06_TRISTRAM"));
        this.cbTristram.setFont(fntCheck);
        if (this.cbLevel02 == null) {
            (this.cbLevel02 = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbLevel02.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_06_LEVEL_02"));
        this.cbLevel02.setFont(fntCheck);
        if (this.cbLevel04 == null) {
            (this.cbLevel04 = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbLevel04.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_06_LEVEL_04"));
        this.cbLevel04.setFont(fntCheck);
        if (this.cbLevel06 == null) {
            (this.cbLevel06 = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbLevel06.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_06_LEVEL_06"));
        this.cbLevel06.setFont(fntCheck);
        if (this.cbLevel08 == null) {
            (this.cbLevel08 = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbLevel08.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_06_LEVEL_08"));
        this.cbLevel08.setFont(fntCheck);
        if (this.cbLevel10 == null) {
            (this.cbLevel10 = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbLevel10.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_06_LEVEL_10"));
        this.cbLevel10.setFont(fntCheck);
        if (this.cbLevel12 == null) {
            (this.cbLevel12 = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbLevel12.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_06_LEVEL_12"));
        this.cbLevel12.setFont(fntCheck);
        if (this.cbLevel14 == null) {
            (this.cbLevel14 = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbLevel14.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_06_LEVEL_14"));
        this.cbLevel14.setFont(fntCheck);
        if (this.cbLevel16 == null) {
            (this.cbLevel16 = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbLevel16.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_06_LEVEL_16"));
        this.cbLevel16.setFont(fntCheck);
        if (this.cbHoradrimTomb == null) {
            (this.cbHoradrimTomb = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbHoradrimTomb.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_06_HORADRIM_TOMB"));
        this.cbHoradrimTomb.setFont(fntCheck);
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }

    public void setSelected(final boolean selected) {
        this.cbShatteredConclave.setSelected(selected);
        this.cbTristram.setSelected(selected);
        this.cbLevel02.setSelected(selected);
        this.cbLevel04.setSelected(selected);
        this.cbLevel06.setSelected(selected);
        this.cbLevel08.setSelected(selected);
        this.cbLevel10.setSelected(selected);
        this.cbLevel12.setSelected(selected);
        this.cbLevel14.setSelected(selected);
        this.cbLevel16.setSelected(selected);
        this.cbHoradrimTomb.setSelected(selected);
    }

    @Override
    public List<GDCharUID> getRiftList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbShatteredConclave.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_06_SHATTERED_CONCLAVE);
        }
        if (this.cbTristram.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_06_TRISTRAM);
        }
        if (this.cbLevel02.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_02);
        }
        if (this.cbLevel04.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_04);
        }
        if (this.cbLevel06.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_06);
        }
        if (this.cbLevel08.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_08);
        }
        if (this.cbLevel10.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_10);
        }
        if (this.cbLevel12.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_12);
        }
        if (this.cbLevel14.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_14);
        }
        if (this.cbLevel16.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_16);
        }
        if (this.cbHoradrimTomb.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_06_HORADRIM_TOMB);
        }
        return list;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRiftList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_06_SHATTERED_CONCLAVE)) {
                this.cbShatteredConclave.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_06_TRISTRAM)) {
                this.cbTristram.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_02)) {
                this.cbLevel02.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_04)) {
                this.cbLevel04.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_06)) {
                this.cbLevel06.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_08)) {
                this.cbLevel08.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_10)) {
                this.cbLevel10.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_12)) {
                this.cbLevel12.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_14)) {
                this.cbLevel14.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_06_LEVEL_16)) {
                this.cbLevel16.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_06_HORADRIM_TOMB)) {
                this.cbHoradrimTomb.setSelected(true);
            }
        }
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            RoTRiftAct6Pane.this.changed = true;
        }
    }
}
